package com.wjq.boot.aop;

import com.wjq.boot.service.EmailService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;

/**
 * ClassName: com.wjq.boot.aop
 * Datetime: 2024/8/9 上午9:54
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
@Component
@Aspect
public class EmailAop {

    @Resource
    private EmailService emailService;

    // 切点后执行的通知
    @AfterReturning( value = "execution(public * com.wjq.boot.service.impl.TAdminServiceImpl.add(..))")
    public void sendEmail(JoinPoint joinPoint) throws MessagingException {
        sendAdmin(joinPoint.getArgs());
    }

    private void sendAdmin(Object[] args) throws MessagingException {
        // 获取收件人
        String to = args[5].toString();
        // 定义主题
        String subject = "系统消息";
        // 定义邮件内容
        String text = "<h2>尊敬的" + args[0].toString() + ":<h2>" +
                "<p>HR已经帮您创建好了系统用户,您可以使用了</p>" +
                "<p>登录名是您的手机号: " + args[4].toString() + "</p>" +
                "<p>登录密码是:" + args[6].toString() + "</p>";
        emailService.send(to,subject,text);
    }
}
